{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Authenticate your Google account (this will open a window in a web brower)\n",
    "from pydrive.auth import GoogleAuth\n",
    "\n",
    "gauth = GoogleAuth()\n",
    "gauth.LocalWebserverAuth()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create GoogleDrive instance\n",
    "from pydrive.drive import GoogleDrive\n",
    "drive = GoogleDrive(gauth)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create and upload a simple file\n",
    "file = drive.CreateFile({'title': 'My Awesome File.txt'})\n",
    "file.SetContentString('Hello World!') # this writes a string directly to a file\n",
    "file.Upload()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "outputPrepend",
     "outputPrepend"
    ]
   },
   "outputs": [],
   "source": [
    "# Search for every file in your Drive not in the trash\n",
    "file_list = drive.ListFile({'q': 'trashed=false'}).GetList()\n",
    "\n",
    "for file in file_list:\n",
    "    print(file['title'], file['id'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "outputPrepend"
    ]
   },
   "outputs": [],
   "source": [
    "# Search for a file with a title that contains \"Hello world\" and is not in the trash\n",
    "# For a full list of how to structure queries, see: https://developers.google.com/drive/api/v2/search-files#python\n",
    "\n",
    "file_list = drive.ListFile({'q': \"title contains 'My Awesome File' and trashed=false\"}).GetList()\n",
    "print(file_list[0]['title']) # should be the title of the file we just created\n",
    "file_id = file_list[0]['id'] # get the file ID"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Download the basic file we just made\n",
    "file = drive.CreateFile({'id': file_id})\n",
    "file.GetContentFile('my-awesome-file.txt') # downloads 'My Awesome File.txt' as 'my-awesome-file.txt'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "file.Trash() # put the simple file in Google Drive trash (not yet deleted)\n",
    "file.UnTrash() # recover from trash\n",
    "file.Delete() # permanently delete (downloaded copy is saved)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Upload arbitrary file\n",
    "cool_image = drive.CreateFile()\n",
    "cool_image.SetContentFile('my-awesome-file.txt') # load local file data into the File instance\n",
    "cool_image.Upload() # creates a file in your drive with the name: my-awesome-file.txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create folder\n",
    "folder = drive.CreateFile({'title': 'My Awesome Folder 123', \"mimeType\": \"application/vnd.google-apps.folder\"})\n",
    "folder.Upload()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Upload file to folder\n",
    "folder = drive.ListFile({'q': \"title = 'My Awesome Folder 123' and trashed=false\"}).GetList()[0] # get the folder we just created\n",
    "file = drive.CreateFile({'title': \"I'm in a folder!.txt\", 'parents': [{'id': folder['id']}]})\n",
    "file.Upload()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Save Credentials (for use in Kaggle or elsewhere)\n",
    "gauth.SaveCredentialsFile(\"mycreds.txt\")"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python37764bitpydrivecondaacb456dbe49947a1b7a1a095c75ccf69",
   "display_name": "Python 3.7.7 64-bit ('pydrive': conda)"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}